home *** CD-ROM | disk | FTP | other *** search
/ Mixa 155: Dogs / MIXA 155: Dogs.iso / pc / Viewer / BROWSER(W) / フィルタ / PDF / LIB / gs_res.ps < prev    next >
Encoding:
Text File  |  2002-10-29  |  28.5 KB  |  910 lines

  1. %    Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is licensed to a single customer by Artifex Software Inc.
  3. % under the terms of a specific OEM agreement.
  4.  
  5. % $RCSfile: gs_res.ps,v $ $Revision: 1.11 $
  6. % Initialization file for Level 2 resource machinery.
  7. % When this is run, systemdict is still writable,
  8. % but (almost) everything defined here goes into level2dict.
  9.  
  10. level2dict begin
  11.  
  12. (BEGIN RESOURCES) VMDEBUG
  13.  
  14. % We keep track of (global) instances with another entry in the resource
  15. % dictionary, an .Instances dictionary.  For categories with implicit
  16. % instances, the values in .Instances are the same as the keys;
  17. % for other categories, the values are [instance status size].
  18.  
  19. % Note that the dictionary that defines a resource category is stored
  20. % in global VM.  The PostScript manual says that each category must
  21. % manage global and local instances separately.  However, objects in
  22. % global VM other than systemdict can't reference objects in local VM.
  23. % This means that the resource category dictionary, which would otherwise be
  24. % the obvious place to keep track of the instances, can't be used to keep
  25. % track of local instances.  Instead, we define a dictionary in local VM
  26. % called localinstancedict, in which the key is the category name and
  27. % the value is the analogue of .Instances for local instances.
  28.  
  29. % We don't currently implement automatic resource unloading.
  30. % When and if we do, it should be hooked to the garbage collector.
  31. % However, Ed Taft of Adobe says their interpreters don't implement this
  32. % either, so we aren't going to worry about it for a while.
  33.  
  34. currentglobal false setglobal systemdict begin
  35.   /localinstancedict 5 dict
  36.   .forcedef    % localinstancedict is local, systemdict is global
  37. end true setglobal
  38. /.emptydict 0 dict readonly def
  39. setglobal
  40.  
  41. % Resource category dictionaries have the following keys (those marked with
  42. % * are optional):
  43. %    Standard, defined in the Red Book:
  44. %        Category (name)
  45. %        *InstanceType (name)
  46. %        DefineResource
  47. %            <key> <instance> DefineResource <instance>
  48. %        UndefineResource
  49. %            <key> UndefineResource -
  50. %        FindResource
  51. %            <key> FindResource <instance>
  52. %        ResourceStatus
  53. %            <key> ResourceStatus <status> <size> true
  54. %            <key> ResourceStatus false
  55. %        ResourceForAll
  56. %            <template> <proc> <scratch> ResourceForAll -
  57. %        *ResourceFileName
  58. %            <key> <scratch> ResourceFileName <filename>
  59. %    Additional, specific to our implementation:
  60. %        .Instances (dictionary)
  61. %        .LocalInstances
  62. %            - .LocalInstances <dict>
  63. %        .GetInstance
  64. %            <key> .GetInstance <instance> -true-
  65. %            <key> .GetInstance -false-
  66. %        .CheckResource
  67. %            <key> <value> .CheckResource <key> <value> <ok>
  68. %              (or may give an error if not OK)
  69. %        .DoLoadResource
  70. %            <key> .DoLoadResource <key> (may give an error)
  71. %        .LoadResource
  72. %            <key> .LoadResource - (may give an error)
  73. %        .ResourceFile
  74. %            <key> .ResourceFile <file> -true-
  75. %            <key> .ResourceFile <key> -false-
  76. %        .ResourceFileStatus
  77. %            <key> .ResourceFileStatus 2 <vmusage> -true-
  78. %            <key> .ResourceFileStatus -false-
  79. % All the above procedures expect that the top dictionary on the d-stack
  80. % is the resource dictionary.
  81.  
  82. % Define enough of the Category category so we can define other categories.
  83. % The dictionary we're about to create will become the Category
  84. % category definition dictionary.
  85.  
  86. % .findcategory and .resourceexec are only called from within the
  87. % implementation of the resource 'operators', so they doesn't have to worry
  88. % about cleaning up the stack if they fail (the interpreter's stack
  89. % protection machinery for pseudo-operators takes care of this).
  90. /.findcategory {    % <name> .findcategory -
  91.             %   (pushes the category on the dstack)
  92.   /Category findresource begin
  93. } bind def
  94.  
  95. /.resourceexec {    % <key> /xxxResource .resourceexec -
  96.             %   (also pops the category from the dstack)
  97.   load exec end
  98. } bind def
  99.  
  100. % .getvminstance treats instances on disk as undefined.
  101. /.getvminstance {    % <key> .getvminstance <instance> -true-
  102.             % <key> .getvminstance -false-
  103.   .GetInstance {
  104.     dup 1 get 2 ne { true } { pop false } ifelse
  105.   } {
  106.     false
  107.   } ifelse
  108. } bind def
  109.  
  110. 20 dict begin
  111.  
  112.         % Standard entries
  113.  
  114. /Category /Category def
  115. /InstanceType /dicttype def
  116.  
  117. /DefineResource {
  118.     .CheckResource {
  119.       dup /Category 3 index cvlit .growput
  120.       dup [ exch 0 -1 ] exch
  121.       .Instances 4 2 roll put
  122.         % Make the Category dictionary read-only.  We will have to
  123.         % use .forceput / .forcedef later to replace the dummy,
  124.         % empty .Instances dictionary with the real one later.
  125.       readonly
  126.     } {
  127.       /defineresource load /typecheck signalerror
  128.     } ifelse
  129. } bind def
  130. /FindResource        % (redefined below)
  131.     { .Instances exch get 0 get
  132.     } bind def
  133.  
  134.         % Additional entries
  135.  
  136. /.Instances 30 dict def
  137. .Instances /Category [currentdict 0 -1] put
  138.  
  139. /.LocalInstances 0 dict def
  140. /.GetInstance
  141.     { .Instances exch .knownget
  142.     } bind def
  143. /.CheckResource
  144.     { dup gcheck currentglobal and
  145.        { /DefineResource /FindResource /ResourceForAll /ResourceStatus
  146.          /UndefineResource }
  147.        { 2 index exch known and }
  148.       forall
  149.       not { /defineresource load /invalidaccess signalerror } if
  150.       true
  151.     } bind def
  152.  
  153. .Instances end begin    % for the base case of findresource
  154.  
  155. (END CATEGORY) VMDEBUG
  156.  
  157. % Define the resource operators.  We use the "stack protection" feature of
  158. % odef to make sure the stacks are restored properly on an error.
  159. % This requires that the operators not pop anything from the stack until
  160. % they have executed their logic successfully.  We can't make this
  161. % work for resourceforall, because the procedure it executes mustn't see
  162. % the operands of resourceforall on the stack, but we can make it work for
  163. % the others.
  164.  
  165. % findresource is the only operator that needs to bind //Category.
  166. /findresource {        % <key> <category> findresource <instance>
  167.     2 copy dup /Category eq
  168.       { pop //Category 0 get begin } { .findcategory } ifelse
  169.     /FindResource .resourceexec exch pop exch pop
  170. } bind
  171. end        % .Instances of Category
  172. odef
  173.  
  174. /defineresource {    % <key> <instance> <category> defineresource <instance>
  175.     3 copy .findcategory
  176.     currentdict /InstanceType known {
  177.       dup type InstanceType ne {
  178.         dup type /packedarraytype eq InstanceType /arraytype eq and
  179.         not { /defineresource load /typecheck signalerror } if
  180.       } if
  181.     } if
  182.     /DefineResource .resourceexec
  183.     4 1 roll pop pop pop
  184. } bind odef
  185. % We must prevent resourceforall from automatically restoring the stacks,
  186. % because we don't want the stacks restored if proc causes an error.
  187. % On the other hand, resourceforall is defined in the PLRM as an operator,
  188. % so it must have type /operatortype.  We hack this by taking advantage of
  189. % the fact that the interpreter optimizes tail calls, so stack protection
  190. % doesn't apply to the very last token of an operator procedure.
  191. /resourceforall1 {    % <template> <proc> <scratch> <category> resourceforall1 -
  192.     dup /Category findresource begin
  193.     /ResourceForAll load
  194.         % Make sure we can recover the original operands.
  195.         % We must create the array in local VM, in case any of the
  196.         % operands are local.
  197.         % Stack: ...operands... proc
  198.     5 copy pop .currentglobal false .setglobal 5 1 roll
  199.     4 packedarray exch .setglobal count
  200.         % Stack: ...operands... proc saved count
  201.     4 -1 roll pop        % pop the category
  202.     /stopped load 3 1 roll
  203.     3 .execn
  204.         % Stack: ... stopped saved count
  205.     3 -1 roll {
  206.       .currentstackprotect {
  207.         % The count is the original stack depth + 2.
  208.         count exch 4 sub sub { exch pop } repeat
  209.         aload pop end
  210.       } {
  211.         % Don't restore the stacks.
  212.         pop pop
  213.       } ifelse stop
  214.     } {
  215.       pop pop end
  216.     } ifelse
  217. } bind def
  218. /resourceforall {    % <template> <proc> <scratch> <category> resourceforall1 -
  219.     //resourceforall1 exec        % see above
  220. } bind odef
  221. /resourcestatus {    % <key> <category> resourcestatus <status> <size> true
  222.             % <key> <category> resourcestatus false
  223.     2 copy .findcategory /ResourceStatus .resourceexec
  224.      { 4 2 roll pop pop true } { pop pop false } ifelse
  225. } bind odef
  226. /undefineresource {    % <key> <category> undefineresource -
  227.     2 copy .findcategory /UndefineResource .resourceexec pop pop
  228. } bind odef
  229.  
  230. % Define the system parameters used for the Generic implementation of
  231. % ResourceFileName.
  232. systemdict begin
  233. currentdict /pssystemparams known not {
  234.   /pssystemparams 10 dict readonly def
  235. } if
  236. pssystemparams begin
  237.   /FontResourceDir (/Resource/Font/) readonly .forcedef    % pssys'params is r-o
  238.   /GenericResourceDir (/Resource/) readonly .forcedef    % pssys'params is r-o
  239.   /GenericResourcePathSep (/) readonly .forcedef    % pssys'params is r-o
  240. end
  241. end
  242.  
  243. % Define the generic algorithm for computing resource file names.
  244. /.rfnstring 100 string def
  245. /.genericrfn        % <key> <scratch> <prefix> .genericrfn <filename>
  246.  { 3 -1 roll //.rfnstring cvs concatstrings exch copy
  247.  } bind def
  248.  
  249. % Define a procedure for making a packed array in local VM.
  250. /.localpackedarray {    % <obj1> ... <objn> <n> .localpackedarray <packedarray>
  251.   .currentglobal false .setglobal 1 index 2 add 1 roll
  252.   packedarray exch .setglobal
  253. } bind def
  254.  
  255. % Define the Generic category.
  256.  
  257. /Generic mark
  258.  
  259.         % Standard entries
  260.  
  261. % We're still running in Level 1 mode, so dictionaries won't expand.
  262. % Leave room for the /Category entry.
  263. /Category null
  264.  
  265. % Implement the body of Generic resourceforall for local, global, and
  266. % external cases.  'args' is [template proc scratch resdict].
  267. /.enumerateresource {    % <key> [- <proc> <scratch>] .enumerateresource -
  268.   1 index type dup /stringtype eq exch /nametype eq or {
  269.     exch 1 index 2 get cvs exch
  270.   } if
  271.     % Use .setstackprotect to prevent the stacks from being restored if
  272.     % an error occurs during execution of proc.
  273.   1 get false .setstackprotect exec true .setstackprotect
  274. } bind def
  275. /.localresourceforall {        % <key> <value> <args> .localr'forall -
  276.   exch pop
  277.   2 copy 0 get .stringmatch { .enumerateresource } { pop pop } ifelse
  278. } bind def
  279. /.globalresourceforall {    % <key> <value> <args> .globalr'forall -
  280.   exch pop
  281.   2 copy 0 get .stringmatch {
  282.     dup 3 get begin .LocalInstances end 2 index known not {
  283.       .enumerateresource
  284.     } if
  285.   } {
  286.     pop pop
  287.   } ifelse
  288. } bind def
  289. /.externalresourceforall {    % <filename> <len> <args> .externalr'forall -
  290.   3 1 roll 1 index length 1 index sub getinterval exch
  291.   dup 3 get begin .Instances .LocalInstances end
  292.         % Stack: key args insts localinsts
  293.   3 index known {
  294.     pop pop pop
  295.   } {
  296.     2 index known { pop pop } { .enumerateresource } ifelse
  297.   } ifelse
  298. } bind def
  299.  
  300. /DefineResource {
  301.     .CheckResource
  302.        { dup [ exch 0 -1 ]
  303.             % Stack: key value instance
  304.          currentglobal
  305.           { false setglobal 2 index UndefineResource    % remove local def if any
  306.         true setglobal
  307.         .Instances dup //.emptydict eq {
  308.           pop 3 dict
  309.             % As noted above, Category dictionaries are read-only,
  310.             % so we have to use .forcedef here.
  311.           /.Instances 1 index .forcedef    % Category dict is read-only
  312.         } if
  313.           }
  314.           { .LocalInstances dup //.emptydict eq
  315.              { pop 3 dict localinstancedict Category 2 index put
  316.          }
  317.         if
  318.           }
  319.          ifelse
  320.             % Stack: key value instance instancedict
  321.          3 index 2 index .growput
  322.             % Now make the resource value read-only.
  323.          0 2 copy get { readonly } .internalstopped pop
  324.          dup 4 1 roll put exch pop exch pop
  325.        }
  326.        { /defineresource load /typecheck signalerror
  327.        }
  328.     ifelse
  329. } bind executeonly        % executeonly to prevent access to .forcedef
  330. /UndefineResource
  331.     {  { dup 2 index .knownget
  332.           { dup 1 get 1 ge
  333.          { dup 0 null put 1 2 put pop pop }
  334.          { pop exch .undef }
  335.         ifelse
  336.           }
  337.           { pop pop
  338.           }
  339.          ifelse
  340.        }
  341.       currentglobal
  342.        { 2 copy .Instances exch exec
  343.        }
  344.       if .LocalInstances exch exec
  345.     } bind
  346. % Because of some badly designed code in Adobe's CID font downloader that
  347. % makes findresource and resourcestatus deliberately inconsistent with each
  348. % other, the default FindResource must not call ResourceStatus if there is
  349. % an instance of the desired name already defined in VM.
  350. /FindResource {
  351.     dup .getvminstance {
  352.       exch pop 0 get
  353.     } {
  354.       dup ResourceStatus {
  355.         pop 1 gt {
  356.           .DoLoadResource .getvminstance not {
  357.         /findresource load /undefinedresource signalerror
  358.           } if 0 get
  359.         } {
  360.           .GetInstance pop 0 get
  361.         } ifelse
  362.       } {
  363.        /findresource load /undefinedresource signalerror
  364.       } ifelse
  365.     } ifelse
  366. } bind
  367. % Because of some badly designed code in Adobe's CID font downloader, the
  368. % definition of ResourceStatus for Generic and Font must be the same (!).
  369. % We patch around this by using an intermediate .ResourceFileStatus procedure.
  370. /ResourceStatus {
  371.     dup .GetInstance {
  372.       exch pop dup 1 get exch 2 get true
  373.     } {
  374.       .ResourceFileStatus
  375.     } ifelse
  376. } bind
  377. /.ResourceFileStatus {
  378.     .ResourceFile { closefile 2 -1 true } { pop false } ifelse
  379. } bind
  380. /ResourceForAll {
  381.         % **************** Doesn't present instance groups in
  382.         % **************** the correct order yet.
  383.         % Construct a new procedure to hold the arguments.
  384.         % All objects constructed here must be in local VM to avoid
  385.         % a possible invalidaccess.
  386.     currentdict 4 .localpackedarray    % [template proc scratch resdict]
  387.         % We must pop the resource dictionary off the dict stack
  388.         % when doing the actual iteration, and restore it afterwards.
  389.     .currentglobal not {
  390.       .LocalInstances length 0 ne {
  391.         % We must do local instances, and do them first.
  392.         //.localresourceforall {exec} 0 get 3 .localpackedarray cvx
  393.         .LocalInstances exch {forall} 0 get 1 index 0 get
  394.         currentdict end 3 .execn begin
  395.       } if
  396.     } if
  397.         % Do global instances next.
  398.     //.globalresourceforall {exec} 0 get 3 .localpackedarray cvx
  399.     .Instances exch cvx {forall} 0 get 1 index 0 get
  400.     currentdict end 3 .execn begin
  401.     currentdict /ResourceFileName known {
  402.         % Finally, do instances stored on files.
  403.       dup 0 get 100 string ResourceFileName
  404.       dup length 2 index 0 get length sub 3 -1 roll
  405.       //.externalresourceforall {exec} 0 get 4 .localpackedarray cvx
  406.       100 string {filenameforall} 0 get
  407.       currentdict end 2 .execn begin null    % for pop
  408.     } if pop
  409. } bind
  410. /ResourceFileName
  411.     { /GenericResourceDir getsystemparam 
  412.       Category .namestring concatstrings
  413.       /GenericResourcePathSep getsystemparam concatstrings
  414.       .genericrfn
  415.     } bind
  416.  
  417.         % Additional entries
  418.  
  419. % Unfortunately, we can't create the real .Instances dictionary now,
  420. % because if someone copies the Generic category (which pp. 95-96 of the
  421. % 2nd Edition Red Book says is legitimate), they'll wind up sharing
  422. % the .Instances.  Instead, we have to create .Instances on demand,
  423. % just like the entry in localinstancedict.
  424. % We also have to prevent anyone from creating instances of Generic itself.
  425. /.Instances //.emptydict
  426.  
  427. /.LocalInstances
  428.     { localinstancedict Category .knownget not { //.emptydict } if
  429.     } bind
  430. /.GetInstance
  431.     { currentglobal
  432.        { .Instances exch .knownget }
  433.        { .LocalInstances 1 index .knownget
  434.           { exch pop true }
  435.           { .Instances exch .knownget }
  436.          ifelse
  437.        }
  438.       ifelse
  439.     } bind
  440. /.CheckResource
  441.     { true
  442.     } bind
  443. /.DoLoadResource {
  444.         % .LoadResource may push entries on the operand stack.
  445.         % It is an undocumented feature of Adobe implementations,
  446.         % which we must match for the sake of some badly written
  447.         % font downloading code, that such entries are popped
  448.         % automatically.
  449.     count 1 index cvlit vmstatus pop exch pop
  450.         % Stack: key count litkey memused
  451.     {.LoadResource} 4 1 roll 4 .execn
  452.         % Stack: ... count key memused
  453.     vmstatus pop exch pop exch sub
  454.     1 index .getvminstance not {
  455.       pop dup /undefinedresource signalerror    % didn't load
  456.     } if
  457.     dup 1 1 put
  458.     2 3 -1 roll put
  459.         % Stack: ... count key
  460.     exch count 1 sub exch sub {exch pop} repeat
  461. } bind
  462. /.LoadResource
  463.     { dup .ResourceFile
  464.        { exch pop currentglobal
  465.           { .runresource }
  466.           { true setglobal { .runresource } stopped false setglobal { stop } if }
  467.          ifelse
  468.        }
  469.        { dup /undefinedresource signalerror
  470.        }
  471.      ifelse
  472.     } bind
  473. /.ResourceFile
  474.     { currentdict /ResourceFileName known
  475.        { mark 1 index 100 string { ResourceFileName }
  476.          .internalstopped
  477.           { cleartomark false }
  478.           { exch pop findlibfile
  479.          { exch pop exch pop true }
  480.          { pop false }
  481.         ifelse
  482.           }
  483.          ifelse
  484.        }
  485.        { false }
  486.       ifelse
  487.     } bind
  488.  
  489. .dicttomark
  490. /Category defineresource pop
  491.  
  492. % Fill in the rest of the Category category.
  493. /Category /Category findresource dup
  494. /Generic /Category findresource begin {
  495.   /FindResource /ResourceForAll /ResourceStatus /.ResourceFileStatus
  496.   /UndefineResource /ResourceFileName
  497.   /.ResourceFile /.LoadResource /.DoLoadResource
  498. } { dup load put dup } forall
  499. pop readonly pop end
  500.  
  501. (END GENERIC) VMDEBUG
  502.  
  503. % Define the fixed categories.
  504.  
  505. mark
  506.     % Non-Type categories with existing entries.
  507.  /ColorSpaceFamily
  508.    { }    % These must be deferred, because optional features may add some.
  509.  /Emulator
  510.    mark EMULATORS { cvn } forall .packtomark
  511.  /Filter
  512.    { }    % These must be deferred, because optional features may add some.
  513.  /IODevice
  514.     % Loop until the .getiodevice gets a rangecheck.
  515.    errordict /rangecheck 2 copy get
  516.    errordict /rangecheck { pop stop } put    % pop the command
  517.    mark 0 { {
  518.     dup .getiodevice dup null eq { pop } { exch } ifelse 1 add
  519.    } loop} .internalstopped
  520.    pop pop pop .packtomark
  521.    4 1 roll put
  522.    .clearerror
  523.     % Type categories listed in the Red Book.
  524.  /ColorRenderingType
  525.    { }    % These must be deferred, because optional features may add some.
  526.  /FMapType
  527.    { }    % These must be deferred, because optional features may add some.
  528.  /FontType
  529.    { }    % These must be deferred, because optional features may add some.
  530.  /FormType
  531.    { }    % These must be deferred, because optional features may add some.
  532.  /HalftoneType
  533.    { }    % These must be deferred, because optional features may add some.
  534.  /ImageType
  535.    { }    % Deferred, optional features may add some.
  536.  /PatternType
  537.    { }  % Deferred, optional features may add some.
  538.     % Type categories added since the Red Book.
  539.  /setsmoothness where {
  540.    pop /ShadingType { }    % Deferred, optional features may add some.
  541.  } if
  542. counttomark 2 idiv
  543.  { mark
  544.  
  545.         % Standard entries
  546.  
  547.         % We'd like to prohibit defineresource,
  548.         % but because optional features may add entries, we can't.
  549.         % We can at least require that the key and value match.
  550.    /DefineResource
  551.     { currentglobal not
  552.        { /defineresource load /invalidaccess signalerror }
  553.        { 2 copy ne
  554.           { /defineresource load /rangecheck signalerror }
  555.           { dup .Instances 4 -2 roll .growput }
  556.          ifelse
  557.        }
  558.       ifelse
  559.     } bind
  560.    /UndefineResource
  561.     { /undefineresource load /invalidaccess signalerror } bind
  562.    /FindResource
  563.     { .Instances 1 index .knownget
  564.        { exch pop }
  565.        { /findresource load /undefinedresource signalerror }
  566.       ifelse
  567.     } bind
  568.    /ResourceStatus
  569.     { .Instances exch known { 0 0 true } { false } ifelse } bind
  570.    /ResourceForAll
  571.     /Generic /Category findresource /ResourceForAll get
  572.  
  573.         % Additional entries
  574.  
  575.    counttomark 2 add -1 roll
  576.    dup length dict dup begin exch { dup def } forall end
  577.         % We'd like to make the .Instances readonly here,
  578.         % but because optional features may add entries, we can't.
  579.    /.Instances exch
  580.    /.LocalInstances    % used by ResourceForAll
  581.     0 dict def
  582.  
  583.    .dicttomark /Category defineresource pop
  584.  } repeat pop
  585.  
  586. (END FIXED) VMDEBUG
  587.  
  588. % Define the other built-in categories.
  589.  
  590. /.definecategory    % <name> -mark- <key1> ... <valuen> .definecategory -
  591.  { counttomark 2 idiv 2 add        % .Instances, Category
  592.    /Generic /Category findresource dup maxlength 3 -1 roll add
  593.    dict .copydict begin
  594.    counttomark 2 idiv { def } repeat pop    % pop the mark
  595.    currentdict end /Category defineresource pop
  596.  } bind def
  597.  
  598. /ColorRendering mark /InstanceType /dicttype .definecategory
  599. % ColorSpace is defined below
  600. % Encoding is defined below
  601. % Font is defined below
  602. /Form mark /InstanceType /dicttype .definecategory
  603. /Halftone mark /InstanceType /dicttype .definecategory
  604. /Pattern mark /InstanceType /dicttype .definecategory
  605. /ProcSet mark /InstanceType /dicttype .definecategory
  606. % Added since the Red Book:
  607. /ControlLanguage mark /InstanceType /dicttype .definecategory
  608. /HWOptions mark /InstanceType /dicttype .definecategory
  609. /Localization mark /InstanceType /dicttype .definecategory
  610. /OutputDevice mark /InstanceType /dicttype .definecategory
  611. /PDL mark /InstanceType /dicttype .definecategory
  612. % CIDFont, CIDMap, and CMap are defined in gs_cidfn.ps
  613. % FontSet is defined in gs_cff.ps
  614. % IdiomSet is defined in gs_ll3.ps
  615. % InkParams and TrapParams are defined in gs_trap.ps
  616.  
  617. (END MISC) VMDEBUG
  618.  
  619. % Define the ColorSpace category.
  620.  
  621. /.defaultcsnames mark
  622.   /DefaultGray 0
  623.   /DefaultRGB 1
  624.   /DefaultCMYK 2
  625. .dicttomark readonly def
  626.  
  627. % The "hooks" are no-ops here, redefined in LL3.
  628. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  629.   pop pop
  630. } bind def
  631. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  632.   pop
  633. } bind def
  634.  
  635. /ColorSpace mark
  636.  
  637. /InstanceType /arraytype
  638.  
  639. % We keep track of whether there are any local definitions for any of
  640. % the Default keys.  This information must get saved and restored in
  641. % parallel with the local instance dictionary, so it must be stored in
  642. % local VM.
  643. userdict /.localcsdefaults false put
  644.  
  645. /DefineResource {
  646.   2 copy /Generic /Category findresource /DefineResource get exec
  647.   exch pop
  648.   exch //.defaultcsnames exch .knownget {
  649.     1 index .definedefaultcs
  650.     currentglobal not { .userdict /.localcsdefaults true put } if
  651.   } if
  652. } bind
  653.  
  654. /UndefineResource {
  655.   dup /Generic /Category findresource /UndefineResource get exec
  656.   //.defaultcsnames 1 index .knownget {
  657.     % Stack: resname index
  658.     currentglobal {
  659.       .undefinedefaultcs pop
  660.     } {
  661.     % We removed the local definition, but there might be a global one.
  662.       exch .GetInstance {
  663.     0 get .definedefaultcs
  664.       } {
  665.     .undefinedefaultcs
  666.       } ifelse
  667.     % Recompute .localcsdefaults by scanning.  This is rarely needed.
  668.       .userdict /.localcsdefaults false //.defaultcsnames {
  669.     pop .LocalInstances exch known { pop true exit } if
  670.       } forall put
  671.     } ifelse
  672.   } {
  673.     pop
  674.   } ifelse
  675. } bind
  676.  
  677. .definecategory            % ColorSpace
  678.  
  679. % Define the Encoding category.
  680.  
  681. /Encoding mark
  682.  
  683. /InstanceType /arraytype
  684.  
  685. % Handle already-registered encodings, including lazily loaded encodings
  686. % that aren't loaded yet.
  687.  
  688. /.Instances mark
  689.   EncodingDirectory
  690.    { dup length 256 eq { [ exch readonly 0 -1 ] } { pop [null 2 -1] } ifelse
  691.    } forall
  692. .dicttomark
  693.  
  694. /.ResourceFileDict mark
  695.   EncodingDirectory
  696.    { dup length 256 eq { pop pop } { 0 get } ifelse
  697.    } forall
  698. .dicttomark
  699.  
  700. /ResourceFileName
  701.  { .ResourceFileDict 2 index .knownget
  702.     { exch copy exch pop }
  703.     { /Generic /Category findresource /ResourceFileName get exec }
  704.    ifelse
  705.  } bind
  706.  
  707. .definecategory            % Encoding
  708.  
  709. % Make placeholders in level2dict for the redefined Encoding operators,
  710. % so that they will be swapped properly when we switch language levels.
  711.  
  712. /.findencoding /.findencoding load def
  713. /findencoding /findencoding load def
  714. /.defineencoding /.defineencoding load def
  715.  
  716. (END ENCODING) VMDEBUG
  717.  
  718. % Define the Font category.
  719.  
  720. /.fontstatus {        % <fontname> .fontstatus <fontname> <found>
  721.   {        % Create a loop context just so we can exit it early.
  722.         % Check Fontmap.
  723.     Fontmap 1 index .knownget {
  724.       {
  725.     dup type /nametype eq {
  726.       .fontstatus { null exit } if
  727.     } {
  728.       dup type /stringtype eq {
  729.         findlibfile { closefile pop null exit } if pop
  730.       } {
  731.         % Procedure, assume success.
  732.         pop null exit
  733.       } ifelse
  734.     } ifelse
  735.       } forall dup null eq { pop true exit } if
  736.     } if
  737.         % Convert names to strings; give up on other types.
  738.     dup type /nametype eq { .namestring } if
  739.     dup type /stringtype ne { false exit } if
  740.         % Check the resource directory.
  741.     dup .fonttempstring /FontResourceDir getsystemparam .genericrfn
  742.     status {
  743.       pop pop pop pop true exit
  744.     } if
  745.         % Check for a file on the search path with the same name
  746.         % as the font.
  747.     findlibfile { closefile true exit } if
  748.         % Scan a FONTPATH directory and try again.
  749.     .scannextfontdir not { false exit } if
  750.   } loop
  751. } bind def
  752.  
  753. /Font mark
  754.  
  755. /InstanceType /dicttype
  756.  
  757. /DefineResource
  758.     { 2 copy //definefont exch pop
  759.       /Generic /Category findresource /DefineResource get exec
  760.     } bind
  761. /UndefineResource
  762.     { dup //undefinefont
  763.       /Generic /Category findresource /UndefineResource get exec
  764.     } bind
  765. /FindResource {
  766.     dup .getvminstance {
  767.       exch pop 0 get
  768.     } {
  769.       dup ResourceStatus {
  770.         pop 1 gt { .loadfontresource } { .GetInstance pop 0 get } ifelse
  771.       } {
  772.         .loadfontresource
  773.       } ifelse
  774.     } ifelse
  775. } bind
  776. /ResourceForAll {
  777.     { .scannextfontdir not { exit } if } loop
  778.     /Generic /Category findresource /ResourceForAll get exec
  779. } bind
  780. /.ResourceFileStatus {
  781.     .fontstatus { pop 2 -1 true } { pop false } ifelse
  782. } bind
  783.  
  784. /.loadfontresource {
  785.     dup vmstatus pop exch pop exch
  786.         % Hack: rebind .currentresourcefile so that all calls of
  787.         % definefont will know these are built-in fonts.
  788.     currentfile {pop //findfont exec} .execasresource  % (findfont is a procedure)
  789.     exch vmstatus pop exch pop exch sub
  790.         % stack: name font vmused
  791.         % findfont has the prerogative of not calling definefont
  792.         % in certain obscure cases of font substitution.
  793.     2 index .getvminstance {
  794.       dup 1 1 put
  795.       2 3 -1 roll put
  796.     } {
  797.       pop
  798.     } ifelse exch pop
  799. } bind
  800.  
  801. /.Instances FontDirectory length 2 mul dict
  802.  
  803. .definecategory            % Font
  804.  
  805. % Redefine font "operators".
  806. /.definefontmap
  807.  { /Font /Category findresource /.Instances get
  808.    dup 3 index known
  809.     { pop
  810.     }
  811.     { 2 index
  812.         % Make sure we create the array in global VM.
  813.       .currentglobal true .setglobal
  814.       [null 2 -1] exch .setglobal
  815.       .growput
  816.     }
  817.    ifelse
  818.    //.definefontmap exec
  819.  } bind def
  820.  
  821. % Make sure the old definitions are still in systemdict so that
  822. % they will get bound properly.
  823. systemdict begin
  824.   /.origdefinefont /definefont load def
  825.   /.origundefinefont /undefinefont load def
  826.   /.origfindfont /findfont load def
  827. end
  828. /definefont {
  829.   /Font defineresource
  830. } bind odef
  831. /undefinefont {
  832.   /Font undefineresource
  833. } bind odef
  834. % The Red Book requires that findfont be a procedure, not an operator,
  835. % but it still needs to restore the stacks reliably if it fails.
  836. /.findfontop {
  837.   /Font findresource
  838. } bind odef
  839. /findfont {
  840.   .findfontop
  841. } bind def    % Must be a procedure, not an operator
  842.  
  843. % Remove initialization utilities.
  844. currentdict /.definecategory .undef
  845. currentdict /.emptydict .undef
  846.  
  847. end                % level2dict
  848.  
  849. % Convert deferred resources after we finally switch to Level 2.
  850.  
  851. /.fixresources {
  852.     % Encoding resources
  853.   EncodingDirectory
  854.    { dup length 256 eq
  855.       { /Encoding defineresource pop }
  856.       { pop pop }
  857.      ifelse
  858.    } forall
  859.   /.findencoding { /Encoding findresource } bind def
  860.   /findencoding /.findencoding load def        % must be a procedure
  861.   /.defineencoding { /Encoding defineresource pop } bind def
  862.     % ColorRendering resources and ProcSet
  863.   systemdict /ColorRendering .knownget {
  864.     /ColorRendering exch /ProcSet defineresource pop
  865.     systemdict /ColorRendering undef
  866.     /Default currentcolorrendering /ColorRendering defineresource pop
  867.   } if
  868.     % ColorSpace resources
  869.   systemdict /CIEsRGB .knownget {
  870.     /sRGB exch /ColorSpace defineresource pop
  871.     systemdict /CIEsRGB undef
  872.   } if
  873.     % ColorSpaceFamily resources
  874.   colorspacedict { pop dup /ColorSpaceFamily defineresource pop } forall
  875.     % Filter resources
  876.   filterdict { pop dup /Filter defineresource pop } forall
  877.     % FontType and FMapType resources
  878.   buildfontdict { pop dup /FontType defineresource pop } forall
  879.   mark
  880.     buildfontdict 0 known { 2 3 4 5 6 7 8 } if
  881.     buildfontdict 9 known { 9 } if
  882.   counttomark { dup /FMapType defineresource pop } repeat pop
  883.     % FormType resources
  884.   .formtypes { pop dup /FormType defineresource pop } forall
  885.     % HalftoneType resources
  886.   .halftonetypes { pop dup /HalftoneType defineresource pop } forall
  887.     % ColorRenderingType resources
  888.   .colorrenderingtypes {pop dup /ColorRenderingType defineresource pop} forall
  889.     % ImageType resources
  890.   .imagetypes { pop dup /ImageType defineresource pop } forall
  891.     % PatternType resources
  892.   .patterntypes { pop dup /PatternType defineresource pop } forall
  893.     % Make the fixed resource categories immutable.
  894.   /.shadingtypes where {
  895.     pop .shadingtypes { pop dup /ShadingType defineresource pop } forall
  896.   } if
  897.   [ /ColorSpaceFamily /Emulator /Filter /IODevice /ColorRenderingType
  898.     /FMapType /FontType /FormType /HalftoneType /ImageType /PatternType
  899.     /.shadingtypes where { pop /ShadingType } if
  900.   ] {
  901.     /Category findresource
  902.     dup /.Instances get readonly pop
  903.     .LocalInstances readonly pop
  904.     readonly pop
  905.   } forall
  906.     % clean up
  907.   systemdict /.fixresources undef
  908. } bind def
  909.